package gui;

import excecoes.ContaException;
import excecoes.IPException;
import excecoes.SistemaException;

import java.util.HashMap;

import sistema.Sistema;

import com.trolltech.qt.gui.QDialog;
import com.trolltech.qt.gui.QHBoxLayout;
import com.trolltech.qt.gui.QIcon;
import com.trolltech.qt.gui.QLabel;
import com.trolltech.qt.gui.QLineEdit;
import com.trolltech.qt.gui.QMessageBox;
import com.trolltech.qt.gui.QPushButton;
import com.trolltech.qt.gui.QVBoxLayout;
import com.trolltech.qt.gui.QLineEdit.EchoMode;

/**
 * Janela para logar
 * 
 * @author 
 *
 */
public class LoginWindow extends QDialog {
	
	private HashMap<String, QLineEdit> editedLines = new  HashMap<String, QLineEdit>();
	private Sistema sistema;
	private final String IMG_PATH = "ImHereM03/img/";
	
	/**
	 * 
	 * @param sistema
	 */
	public LoginWindow(Sistema sistema) {
		this.setWindowTitle("Login");
		this.setFixedSize(300,200);
		this.setLayout(this.createMasterLayout());
		this.sistema = sistema;
		this.setStyleSheet("background: url("+IMG_PATH + "background.png);");
		this.setWindowIcon(new QIcon(IMG_PATH + "icon.png"));
	}
	
	/**
	 * Create the Master Layout
	 * @return
	 */
	private QVBoxLayout createMasterLayout(){
		
		QVBoxLayout vLay = new QVBoxLayout();
		vLay.addLayout(this.createEditableField("Username:"));
		vLay.addLayout(this.createEditableField("Senha:", QLineEdit.EchoMode.Password));
		vLay.addLayout(this.createEditableField("IP:"));
		vLay.addLayout(this.createButtons());
		return vLay;
	}
	
	/**
	 * Create a Editable Field
	 * @param fieldName
	 * @param mode
	 * @return
	 */
	private QHBoxLayout createEditableField(String fieldName, EchoMode mode){
		QLabel description = new QLabel(fieldName);
		description.setFixedHeight(30);
		description.setFixedWidth(70);
		QLineEdit editArea = new QLineEdit();
		editArea.setEchoMode(mode);
		editArea.setFixedHeight(30);
		editArea.setFixedWidth(200);
		QHBoxLayout layout = new QHBoxLayout();
		layout.addWidget(description);
		layout.addWidget(editArea);
		this.editedLines.put(fieldName, editArea);
		return layout;
	}
	
	/**
	 * Create a Editable Field.
	 * @param fieldName
	 * @return
	 */
	private QHBoxLayout createEditableField(String fieldName) {
		return this.createEditableField(fieldName, QLineEdit.EchoMode.Normal);
	}

	/**
	 * create Buttons
	 * @return
	 */
	private QHBoxLayout createButtons(){
		QHBoxLayout hLay = new QHBoxLayout();
		hLay.addWidget(createButton("Login", "login()"));
		hLay.addWidget(createButton("Cancelar", "close()"));
		return hLay;
	}
	
	/**
	 * Create Button
	 * 
	 * @param name
	 * @param callback
	 * @return
	 */
	private QPushButton createButton(String name, String callback){
		QPushButton button = new QPushButton(name);
		button.clicked.connect(this, callback);
		return button;
	}
	
	/**
	 * Callback
	 */
	private void login(){
		String userName = editedLines.get("Username:").text();
		String password = editedLines.get("Senha:").text();
		String ipAddress = editedLines.get("IP:").text();
		try {
			System.out.println(userName);
			System.out.println(password);
			String logado = sistema.login(userName, password, ipAddress);
			if (!logado.isEmpty()) {
				LoggedWindow logged = new LoggedWindow(logado, sistema);
				this.close();
				logged.show();
			}
		} catch (SistemaException e) {
			QMessageBox.critical(this, "Erro", "Senha/Login invalidos");
		} catch (ContaException e) {
			QMessageBox.critical(this, "Erro", "Senha/Login invalidos.");
			
		} catch (IPException e) {
			QMessageBox.critical(this, "Erro", "IP Invalido.");
		} catch (NullPointerException e) {
			QMessageBox.critical(this, "Erro", "Senha/Login invalidos.");
		} 
	}
	
	
}
